package com.car.controller;

import com.car.model.auto.User;
import com.car.service.IUserService;

import com.car.untils.SMS.SMSUtil;
import org.json.JSONObject;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 达瓦里氏
 * @since 2021-11-25
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource
    private IUserService iUserService;

    /*查询用户*/
    @GetMapping("/getUser")
    public List<User> getUser(){
        return iUserService.list();
    }

    /*通过id查询用户*/
    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id") int id){
        return iUserService.getById(id);
    }

    /*修改用户*/
    @PostMapping("/updateUser")
    public void updateUser(@RequestBody User user){
        iUserService.updateById(user);
    }

    /*添加用户*/
    @PostMapping("/setUser")
    public void setUser(@RequestBody User user){
        iUserService.save(user);
    }

    /*删除用户*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id){
        return iUserService.removeById(id);
    }

    //点击发送验证码
    @GetMapping("/sendMs/{phone}")
    public String sendMs (HttpServletRequest request,@PathVariable("phone") String phoneNumber){
        if(phoneNumber!=null&&!phoneNumber.equals("")){
            String s = SMSUtil.sendSMS(request,phoneNumber);
            request.getSession().setAttribute("Code",s);
            System.out.println(s);
            return s;
        }else{
            return "error";
        }
    }

    //点击注册
    @PostMapping("/register")
    public String register(HttpServletRequest request,@RequestBody User user) {
        JSONObject json = (JSONObject)request.getSession().getAttribute("Code");
        System.out.println(json);
        if(!json.equals(user.getCode())){
            return "验证码错误";
        }
        //我这里模拟了一分钟
        if((System.currentTimeMillis() - json.getLong("createTime")) > 1000 * 60 * 1){
            return "验证码过期";
        }
        iUserService.save(user);
        //将用户信息存入数据库、这里省略
        return "success";
    }

    /*后台登录*/
    @PostMapping("/login")
    public int Login(@RequestBody User user){
        User login = iUserService.login(user.getUsername(), user.getPassword());
        if (login!=null){
            int id = login.getId();
            int a = Math.toIntExact(id);
            return a;
        }else {
            return -1;
        }

    }


    /*用户登录*/


    /**
     * 实现文件上传
     * */
    @RequestMapping("/fileUpload")
    @ResponseBody
    public String fileUpload(@RequestParam("fileName") MultipartFile file){
        if(file.isEmpty()){
            return "false";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" + size);

        String path = "D:/test" ;
        File dest = new File(path + "/" + fileName);
        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            System.out.println("上传成功");
            return "true";
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        }
    }
}
